package com.clps.algorithm.chapter08.值和下标之差都在给定的范围内;

import java.util.HashMap;
import java.util.Map;

public class demo02 {
    public static void main(String[] args) {

    }

    public Boolean containNearByAlmostDupLicate(int[] nums,int k,int t){
        Map<Integer, Integer> buckets = new HashMap<>();
        int bucketSize = t + 1;
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            int id = getBucketID(num, bucketSize);

            if (buckets.containsKey(id)
                    || (buckets.containsKey(id-1) && buckets.get(id-1) + t >= num)
                    || (buckets.containsKey(id+1) && buckets.get(id+1) - t <= num)) {
                return true;
            }

            buckets.put(id, num);
            if (i >= k) {
                buckets.remove(getBucketID(nums[i - k], bucketSize));
            }
        }

        return false;
    }

    private int getBucketID(int num, int bucketSize) {
        return num>0?num/bucketSize:(num+1)/bucketSize-1;
    }
}
